﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using Omnia.DataModels;
using Omnia.Library;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Omnia.ViewModels
{
    public class PlaceViewModel : MainViewModel
    {
        private PlaceInfo placeItemToAdd = new PlaceInfo();
        public PlaceInfo PlaceItemToAdd
        {
            get { return placeItemToAdd; }
            set
            {
                placeItemToAdd = value;
                RaisePropertyChanged(() => PlaceItemToAdd);
            }
        }

        private ObservableCollection<PlaceInfo> placeItemsToRemove = new ObservableCollection<PlaceInfo>();
        public ObservableCollection<PlaceInfo> PlaceItemsToRemove
        {
            get { return placeItemsToRemove; }
            set
            {
                placeItemsToRemove = value;
                RaisePropertyChanged(() => PlaceItemsToRemove);
                RaisePropertyChanged(() => RemovePlaceCommand);
            }
        }

        private void Insert(PlaceInfo entity)
        {
            if (!string.IsNullOrWhiteSpace(entity.Place))
            {
                Database.CollectionOfPlaces.InsertOnSubmit(entity);
                Save();
                PlaceItemToAdd = new PlaceInfo();
                RaisePropertyChanged(() => PlaceItemToAdd);
                RaisePropertyChanged(() => StorePlaceCollection);
            }
        }

        private void Remove(IEnumerable<PlaceInfo> entities)
        {
            Database.CollectionOfPlaces.DeleteAllOnSubmit(entities);
            Save();
            RaisePropertyChanged(() => StorePlaceCollection);
        }

        public ICommand AddPlaceCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        Insert(PlaceItemToAdd);
                    }
                };
            }
        }

        public ICommand RemovePlaceCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Remove(PlaceItemsToRemove);
                    }
                };
            }
        }
    }
}
